package com.ks.winterwarmimage.adapter;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ks.winterwarmimage.R;
import com.ks.winterwarmimage.wifi.WiFiTools;

import java.util.List;

/**
 * WiFi列表适配器
 */
public class WiFiAdapter extends BaseAdapter {
    private List<WiFiTools.WiFi> wifiList;
    private Context context;
    public WiFiAdapter(Context context, List<WiFiTools.WiFi> wifiList){
        this.context = context;
        this.wifiList = wifiList;
    }
    public int getCount(){
        return wifiList != null ? wifiList.size() : 0;
    }
    public WiFiTools.WiFi getItem(int position){
        return wifiList.get(position);
    }
    public long getItemId(int position){
        return position;
    }
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if(convertView == null){
            convertView = View.inflate(context, R.layout.wifi_view, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        WiFiTools.WiFi wifi = getItem(position);
        holder.setText(wifi.ssid,wifi.bssid);
        holder.setRssi(wifi.level,wifi.capabilities!=WiFiTools.WiFi.Capabilities.UNENCRYPTED);
        return convertView;
    }
    class ViewHolder{
        private ImageView imgRssi;
        private TextView textSSID,textBssid;
        public ViewHolder(View view){
            view.setTag(this);
            imgRssi= view.findViewById(R.id.img_wifi_rssi);
            textSSID = view.findViewById(R.id.text_wifi_ssid);
            textBssid = view.findViewById(R.id.text_bssid);
        }
        public void setText(String ssid,String bssid){
            textSSID.setText(ssid);
            textBssid.setText(bssid);
        }
        public void setRssi(int rssi,boolean encryption){
            int rssiLevel = WifiManager.calculateSignalLevel(rssi, 5);
            switch (rssiLevel){
                case 0:
                    imgRssi.setImageResource(encryption ? R.drawable.ic_wifi_signal_key_24dp_1 : R.drawable.ic_wifi_signal_24dp_1);
                    break;
                case 1:
                    imgRssi.setImageResource(encryption ? R.drawable.ic_wifi_signal_key_24dp_2 : R.drawable.ic_wifi_signal_24dp_2);
                    break;
                case 2:
                    imgRssi.setImageResource(encryption ? R.drawable.ic_wifi_signal_key_24dp_3 : R.drawable.ic_wifi_signal_24dp_3);
                    break;
                case 3:
                    imgRssi.setImageResource(encryption ? R.drawable.ic_wifi_signal_key_24dp_4 : R.drawable.ic_wifi_signal_24dp_4);
                    break;
                default:
                    imgRssi.setImageResource(encryption ? R.drawable.ic_wifi_signal_key_24dp_5 : R.drawable.ic_wifi_signal_24dp_5);
            }
        }
    }
}
